<?php

/**
 * 这个类是为了兼容之前没有分模块的版本
 * Class MY_Before_Controller
 */
class MY_Before_Controller extends CI_Controller
{
    protected $class_name;
    protected $myModel;
    protected $logsModel;
    protected $method;
    protected $directory;
    protected $prefix_path;

    protected $html_path;  //html目录

    protected $dwt_list;
    protected $dwt_info;
    protected $dwt_info_modal;

    protected $res = array('error' => 0, 'message' => '', 'content' => '', 'append' => '');

    protected $require_login = null;

    protected $datas;
    public $breadcrumb = array();

    /**
     * Constructor function
     * @todo Document more please.
     */
    public function __construct()
    {
        parent::__construct();
        $class_name        = $this->router->class;
        $this->class_name  = $class_name;
        $this->method      = $this->router->method;
        $this->directory   = rtrim($this->router->directory, '/');
        $this->html_path   = trim('html/' . $this->directory . '/', '/') . '/';
        $subfolder         = empty($subfolder) ? $this->directory : $subfolder;
        $this->prefix_path = empty($subfolder) ? '' : $subfolder . '/';
        self::_init($class_name);
    }

    protected function _init($class_name)
    {
        $this->dwt_list       = $this->html_path . $class_name . '_list';
        $this->dwt_info       = $this->html_path . $class_name . '_info';
        $this->dwt_info_modal = $this->html_path . $class_name . '_info_modal.php';

        $this->datas['my_router'] = $this->directory . '/' . $class_name;
        $this->datas['my_class']  = $class_name;
        $this->datas['my_method'] = $this->method;
        $this->datas['my_dir']    = $this->directory;
        $this->datas['full_page'] = true;

        $this->res['interface'] = $class_name . '/' . $this->method;
        $this->load_model($class_name . '_model', true);
    }

    public function load_model($model, $self_model = false)
    {
        $myModel         = $model;
        $model_base_path = APPPATH . 'models/' . $this->prefix_path;
        if (file_exists($model_base_path . $model . '.php') || file_exists($model_base_path . ucfirst($model) . '.php')) {
            $model = $this->prefix_path . $model;
        } else {
            $model_base_path = APPPATH . 'models/';
            if (!file_exists($model_base_path . $model . '.php') && !file_exists($model_base_path . ucfirst($model) . '.php')) {
                $model = null;
            }
        }
        if ($model != null) {
            $this->load->model($model);
            if ($self_model) {
                $this->myModel = $this->$myModel;
            }
        } else {
            $this->load->model('my_ci_model');
            if ($self_model) {
                $this->myModel = $this->my_ci_model;
            }
        }
    }
}
